import java.util.concurrent.CountDownLatch;

public class ThreadDemo23 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch=new CountDownLatch(8);//指定8个”选手“
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                System.out.println("起跑");

                try {
                    //Math.random() 生成的是一个 [0,1) 之间的浮点数
                    Thread.sleep((long) (Math.random()*10000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                latch.countDown();
                System.out.println("撞线！抵达终点");
            }
        };

        for (int i = 0; i <8; i++) {
            Thread t=new Thread(runnable);
            t.start();
        }

        latch.await();
        System.out.println("比赛结束");
    }
}
